<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\facade\Request;

class Role extends Common
{
    /**
     * @param Request $request
     * @param \app\admin\model\Role $model
     * @return \think\response\Json
     */
    public function store(Request $request, \app\admin\model\Role $model)
    {
        $res = $model->save([
            'name' => $request::param('name', ''),
            'description' => $request::param('description', '')
        ]);
        if ($res) {
            return $this->returnJson(200, 'ok', $res);
        } else {
            return $this->returnJson(500, 'fail', $res);
        }
    }

    /**
     * @param Request $request
     * @param \app\admin\model\Role $model
     * @return \think\response\Json
     */
    public function index(Request $request, \app\admin\model\Role $model)
    {
        try {
            $res = $model->page($request::param('page', 1), $request::param('page', 10))
                ->paginate($request::param('page', 10));
            return $this->returnJson(200, 'ok', $res);
        } catch (DbException $e) {
            return $this->returnJson(500, $e->getMessage());
        }
    }

    /**
     * @param $id
     * @param \app\admin\model\Role $model
     * @return \think\response\Json
     */
    public function show($id, \app\admin\model\Role $model)
    {
        try {
            $res = $model->findOrFail($id);
            return $this->returnJson(200, 'ok', $res);
        } catch (DataNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (ModelNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (DbException $e) {
            return $this->returnJson(500, $e->getMessage());
        }
    }

    /**
     * @param $id
     * @param Request $request
     * @param \app\admin\model\Role $model
     * @return \think\response\Json
     */
    public function update($id, Request $request, \app\admin\model\Role $model)
    {
        try {
            $res = $model->findOrFail($id)->save($request::param());
            if ($res) {
                return $this->returnJson(200, 'ok', $res);
            } else {
                return $this->returnJson(500, 'fail', $res);
            }
        } catch (DataNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (ModelNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (DbException $e) {
            return $this->returnJson(500, $e->getMessage());
        }
    }

    /**
     * @param $id
     * @param Request $request
     * @param \app\admin\model\Role $model
     * @return \think\response\Json
     */
    public function delete($id, Request $request, \app\admin\model\Role $model)
    {
        try {
            $res = $model->findOrFail($id)->delete();
            return $this->returnJson(200, 'ok', $res);
        } catch (DataNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (ModelNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (DbException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (\Exception $e) {
            return $this->returnJson(500, $e->getMessage());
        }
    }

    /**
     * @param Admin $adminModel
     * @param \app\admin\model\Role $roleModel
     * @return \think\response\Json
     */
    public function attachRole(Admin $adminModel, \app\admin\model\Role $roleModel)
    {
        try {
            $admin = $adminModel->findOrFail(Request::param('admin', ''));
            $role = $roleModel->findOrFail(Request::param('role', ''));
            $res = $admin->roles()->attach($role->id);
            if ($res) {
                return $this->returnJson(200, 'ok', $res);
            } else {
                return $this->returnJson(500, 'fail', $res);
            }
        } catch (DataNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (ModelNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (DbException $e) {
            return $this->returnJson(500, $e->getMessage());
        }
    }
}
